<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" :rules="rules" v-show="showSearch"
            label-width="88px">
            <el-row>
                <el-col :span="8">
                    <el-form-item label="数据年份" prop="year">
                        <el-date-picker clearable v-model="queryParams.year" type="year" value-format="YYYY"
                            placeholder="请选择数据年份">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="账套" prop="asId">
                        <el-select v-model="queryParams.asId" placeholder="请选择查询账套" clearable style="width: 240px"
                            @change="getProductList">
                            <el-option v-for="dict in as_list" :key="dict.value" :label="dict.label"
                                :value="dict.value" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="果品单价（元/斤）" prop="singlePrice" label-width="150px">
                        <el-input v-model="queryParams.singlePrice" placeholder="请输入人工及打冷费"><template
                                #append>元/斤</template></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-form-item>
                    <el-button type="primary" icon="Search" @click="handleQuery">计算</el-button>
                    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-row>

        </el-form>
        <el-table v-loading="loading" :data="priceCaculateList">
            <el-table-column label="商品名称" align="center" prop="productName" />
            <el-table-column label="销售价合计（元/件）" align="center" prop="totalPrice" />
            <el-table-column label="单件重量（斤）" align="center" prop="productWeight" />
            <el-table-column label="价格明细" align="center">
                <el-table-column label="果品价格" align="center">
                    <el-table-column label="果品单价（元/斤）" align="center" prop="singlePrice" />
                    <el-table-column label="果品单件合计（元）" align="center" prop="fruitTotal" />
                </el-table-column>
                <el-table-column label="包材费（元/件）" align="center" prop="materialsCost" />
                <el-table-column label="人工及打冷价格" align="center">
                    <el-table-column label="人工及打冷费（元/斤）" align="center" prop="chillingFee" />
                    <el-table-column label="人工及打冷费单件合计（元）" align="center" prop="chillingFeeTotal" />
                </el-table-column>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />


    </div>
</template>

<script setup name="Caculator">
import { listProduct } from "@/api/base/product";
import { calculateSalePrice } from "@/api/sales/price";

const { proxy } = getCurrentInstance();
const { as_list } = proxy.useDict('as_list');


const loading = ref(true);
const showSearch = ref(true);
const productList = ref([]);
const priceCaculateList = ref([]);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");


const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 500,
        asId: null,
        productId: null,
        year: null,
    },
    rules: {
        year: [
            { required: true, message: "数据年份不能为空", trigger: "blur" }
        ],
        asId: [
            { required: true, message: "账套不能为空", trigger: "blur" }
        ],
        singlePrice: [
            { required: true, message: "果品单价不能为空", trigger: "blur" }
        ],
    }
});
const { queryParams, rules } = toRefs(data);

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}


/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 查询商品信息列表 */
function getProductList(e) {
    loading.value = true;
    let asId = '200012857'
    if (e) {
        asId = e
    }
    let productQueryParams = {
        pageNum: 1,
        pageSize: 500,
        keywords: "",
        asId: asId
    };
    listProduct(productQueryParams).then(response => {
        productList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}



/** 查询包装费用管理列表 */
function getList() {
    if (queryParams.value.singlePrice == 0) {

    } else {
        loading.value = true;
        calculateSalePrice(queryParams.value).then(response => {
            priceCaculateList.value = response.rows;
            total.value = response.total;
            loading.value = false;
        });
    }
}

getProductList();

</script>